package com.carleasoft.mps.core.exceptions;

import com.carleasoft.mps.core.result.ErrorResult;
import com.carleasoft.mps.core.result.R;
import com.mysql.jdbc.MysqlDataTruncation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * 全局异常拦截处理器
 *
 * @author: haoyi
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 捕获自定义异常
     *
     * @param exception
     * @return ResultBody
     */
    @ExceptionHandler(BusinessException.class)
    public R captureBusinessException(BusinessException exception) {
        log.error(">>>> 出错了 !!! ,错误码：{} , msg：{} ,message: {}<<<<", exception.getCode(), exception.getMsg(), exception.getMessage());
        exception.printStackTrace();
        return R.failed(exception.getCode(), exception.getMsg());
    }

    /**
     * 捕获非法请求异常
     *
     * @param exception
     * @return ResultBody
     */
    @ExceptionHandler(AccessException.class)
    public R captureAccessException(AccessException exception) {
        log.error(">>>> 非法请求异常 !!! ,错误码：{} , msg：{} ,message: {}<<<<", exception.getCode(), exception.getMsg(), exception.getMessage());
        exception.printStackTrace();
        return R.failed(exception.getCode(), exception.getMsg());
    }

    /**
     * 捕获token失效异常
     *
     * @param exception
     * @return ResultBody
     */
    @ExceptionHandler(TokenException.class)
    public R captureTokenException(TokenException exception) {
        log.error(">>>> token失效异常 !!! ,错误码：{} , msg：{} ,message: {}<<<<", exception.getCode(), exception.getMsg(), exception.getMessage());
        exception.printStackTrace();
        return R.failed(exception.getCode(), exception.getMsg());
    }

    /**
     * SQL 错误异常拦截器
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(SQLException.class)
    public R sqlException(SQLException exception) {
        log.error(">>>> SQL 错误异常 !!! , message：{} ,cause: {}<<<< ", exception.getMessage(), exception.getCause());
        exception.printStackTrace();
        return R.result(ErrorResult.SQL_ERROR);
    }

    /**
     * SQL 错误异常拦截器
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(MysqlDataTruncation.class)
    public R sqlException(MysqlDataTruncation exception) {
        log.error(">>>> 字符串过长 !!! , message：{} ,cause: {}<<<< ", exception.getMessage(), exception.getCause());
        exception.printStackTrace();
        return R.result(ErrorResult.SQL_ERROR);
    }

    /**
     * 捕获其余通用异常
     *
     * @param exception
     * @return ResultBody
     */
    @ExceptionHandler(Exception.class)
    public R captureException(Exception exception) {
        log.error(">>>> 出错了 !!! ,message: {} ,cause: {}<<<<", exception.getMessage(), exception.getCause());
        exception.printStackTrace();
        return R.exception(ErrorResult.SYSTEM_ERROR);
    }

    @ExceptionHandler(BaseRunTimeException.class)
    public R captureBaseException(BaseRunTimeException exception) {
        log.error(">>>> 出错了 !!! ,错误码：{} , msg：{} ,message: {} ,cause: {}<<<<", exception.getCode(), exception.getMsg(), exception.getMessage(), exception.getCause());
        exception.printStackTrace();
        return R.failed(exception.getCode(), exception.getMsg());
    }

    @ExceptionHandler(ParamException.class)
    public R captureParamException(ParamException exception) {
        log.error(">>>> 参数错误 !!! ,错误码：{} , message：{} ,cause: {}<<<< ", exception.getCode(), exception.getMessage(), exception.getCause());
        exception.printStackTrace();
        return R.failed(exception.getCode(), exception.getMsg());
    }


}
